package com.turboconnard.display;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Vector;

import javax.microedition.khronos.opengles.GL10;

import com.turboconnard.geom.Point3D;

public class Stroke3D extends Sprite {
	protected int count; 
	protected Vector<Point3D> vPoints;
	protected float[] points;
	protected FloatBuffer vertexBuffer;
	protected ByteBuffer byteBuf;
	protected int mode = GL10.GL_LINE_STRIP;
	/** The buffer holding the vertices */
	
	public Stroke3D() {
		count = 0;
		vPoints = new Vector<Point3D>();
		points = null;// new float[0];
	}
	public void addPoint(float pX, float pY, float pZ){
		vPoints.add( new Point3D(pX,pY,pZ) );
		//float[] vert = new float[vPoints.size()*3];
		count++;
		points = new float[(count*3)];
		int c = 0;
		for (Point3D p : vPoints) {
			points[c] = p.x*stage.correction;
			points[c+1] = -p.y*stage.correction;
			points[c+2] = p.z*stage.correction;
			c+=3;
		}

		byteBuf = ByteBuffer.allocateDirect(points.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());		
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.clear ();
		vertexBuffer.put(points);
		vertexBuffer.position(0);
	}
	@Override
	protected void _draw(GL10 gl) {
		if( points==null) return;
		if(points.length<=0) return;
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	//	gl.glFrontFace(GL10.GL_CCW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glDrawArrays(mode, 0, points.length/3);// indexBuffer);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
	}
}
